//
//  luoerNewFeatureViewController.swift
//  WeiBoStudy
//
//  Created by luoerliu on 16/5/16.
//  Copyright © 2016年 刘文平. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let ItemCount = 4
class luoerNewFeatureViewController: UICollectionViewController {
//layout属性
    private let layout = UICollectionViewFlowLayout()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init()
    {
        super.init(collectionViewLayout:layout)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.prepareCollectionView()
    }
    private func prepareCollectionView()
    {
        self.collectionView!.registerClass(luoerNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
//    设置collectionView的布局属性
//        让item和屏幕一样大
        self.layout.itemSize = UIScreen.mainScreen().bounds.size
//        设置为水平滚动
        self.layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
//        设置item之间的间距为0
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
//        取消弹簧效果
        collectionView?.bounces = false
//       分页效果
        collectionView?.pagingEnabled = true
    }


}
//MARK：----数据源方法
extension luoerNewFeatureViewController
{
//    一组有几个cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ItemCount
    }
//    每一个cell是什么样
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! luoerNewFeatureCell
//        cell.backgroundColor = UIColor.yellowColor()
//        拿到cell的索引
       cell.index = indexPath.item
        return cell
    }
    /// scrollView停止滚动
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 获取collectionView的偏移值,计算出当前停下来的是哪一页
        let index = Int(scrollView.contentOffset.x / scrollView.frame.width)
        // 判断是否是最后一页
        if index == ItemCount - 1 {
            // 获取cell,千万不要自己去创建cell,应该拿到正在显示的cell
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! luoerNewFeatureCell
            cell.startAnimation()
        }
    }
 
}
